package com.easy.demo.demos.web;

import com.easy.demo.demos.web.service.LeaveService;
import com.easy.demo.demos.web.service.UserService;
import com.easy.demo.demos.web.vo.Leave;
import com.easy.activiti.util.AjaxResponse;
import com.easy.activiti.util.GlobalConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhouchuang
 * @date 2024-11-12 23:42:30
 */
@RestController
@RequestMapping("/leave")
public class LeaveController {
    @Autowired
    private LeaveService leaveService;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public AjaxResponse list(){
        String username = userService.me().getUsername();
        List<Leave> list =  leaveService.leaves().stream().filter(t->t.getUsername().equals(username)).collect(Collectors.toList());
        return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
                GlobalConfig.ResponseCode.SUCCESS.getDesc(), list);
    }

    @PostMapping("/add")
    public AjaxResponse add(Leave leave){
        return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
                GlobalConfig.ResponseCode.SUCCESS.getDesc(), leaveService.addLeave(leave.getDays(), leave.getReason()));
    }

    @GetMapping("/get")
    public AjaxResponse get(@RequestParam("id") String id){
       return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
                GlobalConfig.ResponseCode.SUCCESS.getDesc(), leaveService.get(id));
    }
}
